import scrapy
from audio.items import AudioItem

class TukuSpider(scrapy.Spider):
    name = 'tuku'
    allowed_domains = ['tukuppt.com']
    start_urls = ['https://www.tukuppt.com/']
    
    title = ["peiyue", "yinxiao"]
    # yinxiao
    scene1 = {"m101": "影视音效", "m102": "游戏音效", "m103": "动画音效", "m104": "其他"}
    # scene1 = {"m101": "影视音效"}

    # peiyue
    scene2 = {"m105": "情绪场景", "m106": "影视配乐", "m107": "乐曲曲风", "m108": "婚礼音乐", "m109": "游戏配乐", "m110": "其他"}
    # scene2 = {"m105": "情绪场景"}
    # peiyue/m109/
    # https://www.tukuppt.com/yinxiao/zonghe_0_0_0_0_0_0_3.html
    # https://www.tukuppt.com/peiyue/m109/zonghe_0_0_0_0_0_0_2.html
    
    def start_requests(self):
        for key, value in self.scene1.items():
            type = "yinxiao"
            url = self.start_urls[0] + type + "/" + key
            params = {"title": "音效", "sub": value,
                      "titleType": type, "subType": key}
            yield scrapy.Request(url=url, method="GET", callback=self.parse, meta=params)
        
        # for key, value in self.scene2.items():
        #     type = "peiyue"
        #     url = self.start_urls[0] + type + "/" + key
        #     params = {"title": "配音", "sub": value,
        #               "titleType": type, "subType": key}
        #     yield scrapy.Request(url=url, method="GET", callback=self.parse, meta=params)
        
    
    def parse(self, response):
        path = "mp3/{}/{}/".format(response.meta["title"], response.meta["sub"])
        list = response.xpath('//dl[@class="cbox audio-box "]')
        
        for row in list:
            url = "http:" + row.xpath('./audio/source/@src').get()
            name = row.xpath('./dt/a/text()').get()
            item = AudioItem()
            item['url'] = url
            item['name'] = name
            item["path"] = path
            yield item
        
        # 页码
        pages = response.xpath(
            '//div[@class="pageinfo"]/a[@rel="nofollow"]/text()')
        totalPage = pages[-2].get()
        if int(totalPage) > 5:
            totalPage = 5
        for page in range(2, int(totalPage)+1):
            pageUrl = "https://www.tukuppt.com/{}/{}/zonghe_0_0_0_0_0_0_{}.html".format(
                response.meta["titleType"], response.meta["subType"], str(page))
            yield scrapy.Request(url=pageUrl, method="GET", callback=self.parse_page, meta=response.meta)

    def parse_page(self, response):
        path = "mp3/{}/{}/".format(response.meta["title"], response.meta["sub"])
        list = response.xpath('//dl[@class="cbox audio-box "]')
        for row in list:
            url = "http:" + row.xpath('./audio/source/@src').get()
            name = row.xpath('./dt/a/text()').get()
            item = AudioItem()
            item['url'] = url
            item['name'] = name
            item["path"] = path
            yield item
